<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Dao;

use App\Model\UserBank;

/**
 * 用户银行卡 Dao
 *
 * @author 张昊(2396675557@qq.com)
 * @package App\Dao
 */
class UserBankDao extends Dao
{
    /**
     * get
     *
     * @param array $map
     * @return mixed
     */
    public function get(array $map = [])
    {
        return UserBank::query()
            ->where(function($query) use ($map) {
                return queryWhere($query, $map);
            })
            ->where('user_id', uid())
            ->select('id', 'bank_name', 'bank_account', 'name', 'idcard', 'phone')
            ->orderByDesc('created_at')
            ->get();
    }

    /**
     * first
     *
     * @param int $id
     * @param bool $abnormal
     * @return mixed
     */
    public function first(int $id, bool $abnormal = true)
    {
        $first = UserBank::query()->where('id', $id)->first();

        if (!$first && $abnormal === true) {
            $this->error('BANK.NOT_FOUND');
        }

        return $first;
    }

    /**
     * create
     *
     * @param array $data
     * @return mixed
     */
    public function create(array $data)
    {
        return UserBank::query()->create($data);
    }

    /**
     * delete
     *
     * @param int $bank_id
     * @return int
     */
    public function delete(int $bank_id)
    {
        return UserBank::query()->where('id', $bank_id)->delete();
    }
}